package com.util;

import java.util.HashMap;
import java.util.Map;

import com.qy.util.SystemParamsUtil;

import main.java.com.github.wxpay.sdk.WXPay;
import main.java.com.github.wxpay.sdk.WXPayUtil;



public class QinYiWXPay {

    private WXPay wxpay;
    private WXPayConfigImpl config;
    private String out_trade_no;
    private String total_fee;
    private String ip;
    private String openId;
    /**
     * 支付类
     * @param amount  支付金额
     * @param ip      发起ip
     * @param openId  第三方id
     * @param localNo 本地的订单号
     * @throws Exception
     */
    public QinYiWXPay(int amount,String ip,String openId,String localNo) throws Exception {
        config = WXPayConfigImpl.getInstance();
        wxpay = new WXPay(config);
        total_fee = amount+"";
        out_trade_no = localNo;
        this.ip = ip;
        this.openId = openId;
    }
    /**
     * 统一  下单
     * @throws Exception 
     */
    public Map<String, String> doUnifiedOrder() throws Exception {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("body", SystemParamsUtil.getWXBody());
        data.put("out_trade_no", out_trade_no);
        data.put("fee_type", "CNY");
        data.put("total_fee", total_fee+"");
        data.put("spbill_create_ip", ip);
        data.put("notify_url",SystemParamsUtil.getWXNotifyUrl());
        data.put("trade_type", "JSAPI");
        data.put("openid", openId);
        Map<String, String> r = null;
        try {
           r = wxpay.unifiedOrder(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return r;
    }


    public void doOrderClose() {
        System.out.println("关闭订单");
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
        try {
           wxpay.closeOrder(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doOrderQuery() {
        System.out.println("查询订单");
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
//        data.put("transaction_id", "4008852001201608221962061594");
        try {
           wxpay.orderQuery(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doOrderReverse() {
        System.out.println("撤销");
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
//        data.put("transaction_id", "4008852001201608221962061594");
        try {
           wxpay.reverse(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 长链接转短链接
     * 测试成功
     */
    public void doShortUrl() {
        String long_url = "weixin://wxpay/bizpayurl?pr=etxB4DY";
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("long_url", long_url);
        try {
           wxpay.shortUrl(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 退款
     * 已测试
     */
    public  Map<String, String> doRefund(String returnFee) {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
        data.put("out_refund_no", out_trade_no);
        data.put("total_fee", total_fee);
        data.put("refund_fee", returnFee);
        data.put("refund_fee_type", "CNY");
        data.put("op_user_id", config.getMchID());
        Map<String, String> r = null;
        try {
          r = wxpay.refund(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return r;

    }

    /**
     * 查询退款
     * 已经测试
     */
    public void doRefundQuery() {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_refund_no", out_trade_no);
        try {
           wxpay.refundQuery(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 对账单下载
     * 已测试
     */
    public void doDownloadBill() {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("bill_date", "20161102");
        data.put("bill_type", "ALL");
        try {
           wxpay.downloadBill(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doGetSandboxSignKey() throws Exception {
        WXPayConfigImpl config = WXPayConfigImpl.getInstance();
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("mch_id", config.getMchID());
        data.put("nonce_str", WXPayUtil.generateNonceStr());
        String sign = WXPayUtil.generateSignature(data, config.getKey());
        data.put("sign", sign);
        WXPay wxPay = new WXPay(config);
//        String result = 
        		wxPay.requestWithoutCert("https://api.mch.weixin.qq.com/sandbox/pay/getsignkey", data, 10000, 10000);
    }

    public static void main(String[] args) throws Exception {
        System.out.println("--------------->");
        QinYiWXPay dodo = new QinYiWXPay(1,"101.130.117.135","o9V_pvtebOiXBgSyZxR71-yO-yEU","test");
        dodo.doUnifiedOrder();
        System.out.println("<---------------"); // wx2016112510573077
    }
}
